import gugga.common.ITask;
import gugga.debug.Assertion;
import gugga.events.EventDispatcher;
import gugga.utils.EventRethrower;

/**
 * @author stefan
 */
class gugga.sequence.AnonymousTask 
	extends EventDispatcher implements ITask 
{
	private var mObject : Object;
	private var mMethod : Function;	private var mArguments : Array;
	
	private var mTask : ITask;
	
	public function AnonymousTask (aObject : Object, aMethod : Function, aArguments : Array)
	{
		mObject = aObject;
		mMethod = aMethod;
		mArguments = aArguments;	
	}
	
	public function start() : Void 
	{
		mTask = ITask(mMethod.apply(mObject, mArguments));
		
		Assertion.warningIfEmpty(mTask, "Anonymous task is empty.", this, arguments);
		
		EventRethrower.create(this, mTask, "completed");
		EventRethrower.create(this, mTask, "interrupted");
		EventRethrower.create(this, mTask, "start");
		
		mTask.start();
	}

	public function isRunning() : Boolean 
	{
		return mTask.isRunning();
	}

	public function isImmediatelyInterruptable() : Boolean 
	{
		return mTask.isImmediatelyInterruptable();
	}

	public function interrupt() : Void 
	{
		mTask.interrupt();
	}
}